// devops/pages/pile/gun.js
import service from '../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pileId: '',
    siteId: '',
    name: '',
    dataList: [],
    siteName: '龙城国际测试测试'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.siteId){
      this.setData({
        siteId: options.siteId,
        name: options.name,
        pileId: options.pileId
      })
      this.getList()
    }
  },
  getList(type) {
    let that = this
    service("POST",`/devops/power/box/device/site/${this.data.siteId}`).then(res => {
      console.log('res', res)
      if(res.data.code ==0){
        let data = res.data.list
        if(data.length >0){
          data = [{name: this.data.siteName,childs: data, type: 'site'}]
          let dataList = that.handleData(data)
          that.setData({
            dataList: dataList
           })
           console.log('dataList',dataList)
         }  
      
      }
    })
  },
  handleData(data){
    let that = this
    return data.map((item, index) => {
      if(item.childs){
        item.num = item.childs.length
        item.percent = ( 100 /item.num ).toFixed(0)
        item.childs = that.handleData(item.childs)
      }
      return item
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})